function res = date2toy (id, cal)

   ## usage:  res = date2toy (id, cal)
   ##
   ## calculate time of year in radians

   n = rows(id) ;

   y = id(:,1) ; m = id(:,2) ; d = id(:,3) ;
   if columns(id) > 3
      h = id(:,4) ;
   else
      h = 12 * ones(rows(id), 1) ;
   endif 

   if strcmp(cal, "360_day") || strfind(cal, "360")
      per = repmat(360, n, 1) ;
      cmon = [0 repmat(30, 1, 11)] ;
      cmon = repmat(cmon, n, 1) ; 
   else
      per = repmat(365, n, 1) ;
      cmon = [0 31 28 31 30 31 30 31 31 30 31 30] ;
      cmon = repmat(cmon, n, 1) ; 
      if (strcmp(cal, "gregorian"))
	 I = (yeardays(y) == 366) ; per(I) = 366 ; cmon(I,3) = 29 ;
      endif 
   endif 
   cmon = cumsum(cmon, 2) ;

   I = repmat(1:12, n, 1) == repmat(m, 1, 12) ;
   res = cmon'(I') + d ; # day of year
   res = 2 * pi * ((res - 1) * 24 + h) ./ (per * 24) ;

endfunction
